# import ee

# # 进行身份验证和初始化
# try:
#     ee.Authenticate()
# except ee.EEException as e:
#     if 'credentials' in str(e):
#         print("Credentials already exist. Use --force to refresh.")
#     else:
#         print("An error occurred: ", str(e))

# ee.Initialize(project='ee-gohoriver')

# # 使用最新的MODIS NDVI数据集（版本061）
# modisNDVI = ee.ImageCollection('MODIS/061/MOD13Q1')  # 更新数据集版本

# # 定义湖南省边界
# hunan_geometry = ee.Geometry.Polygon([
#     [110.26, 24.34], [110.26, 29.67],
#     [114.52, 29.67], [114.52, 24.34]
# ])

# # 处理NDVI数据
# ndvi2021 = modisNDVI.filterDate('2021-01-01', '2021-12-31') \
#                    .select('NDVI') \
#                    .mean() \
#                    .multiply(0.0001)  # 应用缩放系数（将原始值转换为-0.2~1.0范围）

# # 裁剪到湖南省边界
# ndviClip = ndvi2021.clip(hunan_geometry)

# # 设置导出参数（使用关键字参数形式）
# exportTask = ee.batch.Export.image.toDrive(
#     image=ndviClip,
#     description='ndvi_2021_hunan',
#     folder='GEE_Export',
#     fileNamePrefix='hunan_ndvi_2021',
#     region=hunan_geometry,
#     scale=250,  # MODIS原生分辨率
#     crs='EPSG:4326',
#     maxPixels=1e13,
#     fileFormat='GeoTIFF'
# )

# # 启动导出任务
# exportTask.start()
# print(f'Export task started. Task ID: {exportTask.id}')